#!/bin/bash

arch="x86"
system="all"
current_pwd=""
package_file=""
packages=""
log_file=""
depends_file=""
download_path=""
grep_depends="Depends"

function error() {
    local date=$(date)
    local para=$1
    echo -e "\033[31m[$date] error : $para \033[0m"
}

function info() {
    local date=$(date)
    local para=$1
    echo -e "\033[32m[$date] info : $para \033[0m"
}

function do_get_language() {
    # 是中文
    if [[ $LANG =~ "zh_CN" ]]; then
        grep_depends="依赖"
    fi
    # 是英文
    if [[ $LANG =~ "en_US" ]]; then
        grep_depends="Depends"
    fi
}

function do_get_system_id() {
    source /etc/os-release
    system="$ID-$VERSION_ID"
}

function do_generate_arch() {
    hostnamectl | grep -i "Architecture: x86"
    if [ $? -ne 0 ]; then
        hostnamectl | grep -i "Architecture: arm"
        if [ $? -ne 0 ]; then
            exit 1
        fi
        arch="arm"
    else
        arch="x86"
    fi
}

function do_configure() {
    current_pwd=$(
        cd "$(dirname "$0")"
        pwd
    )

    do_get_language
    do_generate_arch
    do_get_system_id

    if [ " ${package_file}" == " " ]; then
        package_file=${current_pwd}/package.txt
    fi

    download_path=${current_pwd}/${arch}/${system}
    log_file=${download_path}/download.log
    depends_file=${download_path}/depends.txt

    mkdir -p ${download_path}/archives

    rm -f $log_file
    rm -f $depends_file
    cd ${download_path}/archives
}

function do_get_packages() {
    if [ ! -f "${package_file}" ]; then
        error "not find package : ${package_file}"
    else
        for package in $(cat $package_file); do
            packages="${packages} ${package}"
        done
        info "packages : \n$packages\n"
    fi
}

function get_depends() {
    echo $1 >>$log_file
    ret=$(apt-cache depends $1 | grep $grep_depends | cut -d: -f2 | tr -d "<>")
    echo $ret | tee -a $log_file
    echo "$ret" >>$depends_file
}

function get_depends_list() {
    if [ " $1" != " " ]; then
        package=$1
        depends=""
        tmp_depends_file=${download_path}/${package}/${package}.txt
        if [ -f "${tmp_depends_file}" ]; then
            for depend in $(cat $tmp_depends_file); do
                depends="${depends} ${depend}"
            done
            echo $depends
        fi
    fi
}

# 递归下载
function download_package_recursion_self() {
    i=0
    libs="$@"
    while [ $i -lt 3 ]; do
        let i++
        newlist=" "
        for package in $libs; do
            info "$package"
            dependen="$(get_depends $package)"
            newlist="$newlist $dependen"
            apt download $dependen
            newlist=$(echo $newlist | sed 's/ /\n/g' | sort -u)
        done
        libs=$newlist
    done
}

# 递归下载
function download_package_recursion() {
    libs="$@"
    apt download $libs
    for package in $libs; do
        info "$package"
        dependen_list="$(get_depends_list $package)"
        echo $dependen_list
        if [ " $dependen_list" != " " ]; then
            apt download $dependen_list
        else
            download_package_recursion_self ${package}
        fi
    done
}


function do_append_packages() {
    for package in $@; do
        info "download package : ${package}"
        download_package_recursion ${package}
        if [ $? -ne 0 ]; then
            error "can't download package : ${package}"
        else
            echo "${package}" >>$package_file
        fi
    done
}

function do_sort_packages() {
    sort -u $package_file >$package_file.tmp
    cp $package_file.tmp $package_file
    rm $package_file.tmp
}

main() {
    do_configure
    do_append_packages $@
    do_sort_packages
    do_get_packages
    download_package_recursion ${packages}
}

main "$@"
